﻿using System;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

namespace Core
{
    public interface IAssetHelper : IHelperTag
    {
        AssetBundle LoadAssetBundle(string path);
        ETTask<AssetBundle> LoadAssetBundleAsync(string path);
        void UnloadAssetBundle(AssetBundle assetBundle);
        string LoadString(string path);
        ETTask<string> LoadStringAsync(string path, int timeout = 0);
        void SaveString(string path, string content);
        ETTask SaveStringAsync(string path, string content);
        void AppendString(string path, string content);
        ETTask AppendStringAsync(string path, string content);
        byte[] LoadBytes(string path);
        ETTask<byte[]> LoadBytesAsync(string path, int timeout = 0);
        void SaveBytes(string path, byte[] bytes);
        ETTask SaveBytesAsync(string path, byte[] bytes);
        bool IsFileExists(string path);
        bool IsDirectoryExists(string path);
        void DeleteFile(string path);
        void DeleteDirectory(string path);
    }

    public class AssetHelper : Helper, IAssetHelper
    {
        #region 文本

        public string LoadString(string path)
        {
            try
            {
                return File.ReadAllText(path, System.Text.Encoding.UTF8);
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
                return null;
            }
        }

        public async ETTask<string> LoadStringAsync(string path, int timeout = 0)
        {
            var request = UnityWebRequest.Get(path);
            if (timeout != 0)
                request.timeout = timeout;

            await request.SendWebRequest();

            string content = "";

            if (request.result == UnityWebRequest.Result.Success)
            {
                content = request.downloadHandler.text;
            }
            else
            {
                Debug.LogError(request.error);
            }

            request.Dispose();
            return content;
        }

        public void SaveString(string path, string content)
        {
            var folderPath = Path.GetDirectoryName(path);
            if (!Directory.Exists(folderPath))
                Directory.CreateDirectory(folderPath);

            File.WriteAllText(path, content);
        }

        public async ETTask SaveStringAsync(string path, string content)
        {
            var folderPath = Path.GetDirectoryName(path);
            if (!Directory.Exists(folderPath))
                Directory.CreateDirectory(folderPath);

            await File.WriteAllTextAsync(path, content);
        }

        public void AppendString(string path, string content)
        {
            var folderPath = Path.GetDirectoryName(path);
            if (!Directory.Exists(folderPath))
                Directory.CreateDirectory(folderPath);

            File.AppendAllText(path, content);
        }

        public async ETTask AppendStringAsync(string path, string content)
        {
            var folderPath = Path.GetDirectoryName(path);
            if (!Directory.Exists(folderPath))
                Directory.CreateDirectory(folderPath);

            await File.AppendAllTextAsync(path, content);
        }

        #endregion

        #region 二进制

        public byte[] LoadBytes(string path)
        {
            try
            {
                return File.ReadAllBytes(path);
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
                return null;
            }
        }

        public async ETTask<byte[]> LoadBytesAsync(string path, int timeout = 0)
        {
            var request = UnityWebRequest.Get(path);
            if (timeout != 0)
                request.timeout = timeout;

            await request.SendWebRequest();

            byte[] content = null;

            if (request.result == UnityWebRequest.Result.Success)
            {
                content = request.downloadHandler.data;
            }
            else
            {
                Debug.LogError(request.error);
            }

            request.Dispose();
            return content;
        }

        public void SaveBytes(string path, byte[] bytes)
        {
            var folderPath = Path.GetDirectoryName(path);
            if (!Directory.Exists(folderPath))
                Directory.CreateDirectory(folderPath);

            File.WriteAllBytes(path, bytes);
        }

        public async ETTask SaveBytesAsync(string path, byte[] bytes)
        {
            var folderPath = Path.GetDirectoryName(path);
            if (!Directory.Exists(folderPath))
                Directory.CreateDirectory(folderPath);

            await File.WriteAllBytesAsync(path, bytes);
        }

        #endregion

        public bool IsFileExists(string path)
        {
            return File.Exists(path);
        }

        public void DeleteFile(string path)
        {
            try
            {
                if (File.Exists(path))
                    File.Delete(path);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        public void DeleteDirectory(string path)
        {
            try
            {
                if (Directory.Exists(path))
                    Directory.Delete(path, true);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        public bool IsDirectoryExists(string path)
        {
            return Directory.Exists(path);
        }

        public AssetBundle LoadAssetBundle(string path)
        {
            return AssetBundle.LoadFromFile(path);
        }

        public async ETTask<AssetBundle> LoadAssetBundleAsync(string path)
        {
            return await AssetBundle.LoadFromFileAsync(path);
        }

        public void UnloadAssetBundle(AssetBundle assetBundle)
        {
            assetBundle.Unload(true);
        }
    }
}
